package com.yjkf.zh.admin.service.impl;

import com.yjkf.zh.admin.dao.SysMenuMapper;
import com.yjkf.zh.admin.dao.SysRoleMapper;
import com.yjkf.zh.admin.dao.SysRoleMenuMapper;
import com.yjkf.zh.admin.dao.SysUserRoleMapper;
import com.yjkf.zh.admin.model.SysMenu;
import com.yjkf.zh.admin.model.SysRole;
import com.yjkf.zh.admin.model.SysRoleMenu;
import com.yjkf.zh.admin.model.SysUserRole;
import com.yjkf.zh.admin.service.SysRoleService;
import com.yjkf.zh.admin.util.SysConstant;
import com.yjkf.zh.core.page.MybatisPageHelper;
import com.yjkf.zh.core.page.PageRequest;
import com.yjkf.zh.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleSericeImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    //@CachePut(value = "role")
    @Override
    public int save(SysRole record) {
        if(record.getId()==null||record.getId()==0){
            return sysRoleMapper.insertRole(record);
        }else{
            return sysRoleMapper.updateByRole(record);
        }

    }

   // @CacheEvict(value = "role")
    @Override
    public int delete(SysRole record) {
        //根据Id获取userrole
        sysUserRoleMapper.deleteByRoleId(record.getId());
        sysRoleMenuMapper.deleteByRoleId(record.getId());
        return sysRoleMapper.deleteRole(record.getId());
    }

    @Override
    public int delete(List<SysRole> records) {
        for(SysRole sysRole:records){
            delete(sysRole);
        }
        return 1;
    }

   // @Cacheable(value = "role",key = "#id")
    @Override
    public SysRole findById(Long id) {
        return null;
    }

  //  @Cacheable(value = "role")
    @Override
    public PageResult findPage(PageRequest pageRequest) {
        PageResult pageResult = null;
       /* Object id = pageRequest.getParam("id");
        Object roleName = pageRequest.getParam("roleName");
        if(roleName!=null){
            pageResult=MybatisPageHelper.findPage(pageRequest,sysRoleMapper,"findRolePageByList",roleName,id);
        }else{

            pageResult=MybatisPageHelper.findPage(pageRequest,sysRoleMapper,"findRolePageList",id);
        }*/
       pageResult = MybatisPageHelper.findPage(pageRequest,sysRoleMapper,"findAllPage");
        return  pageResult;
    }

   // @Cacheable(value = "role",key = "#id")
    @Override
    public SysRole findRoleById(Long id) {
        return sysRoleMapper.findRoleById(id);
    }

  //  @Cacheable(value = "role",key = "#roleName")
    @Override
    public List<SysRole> findRoleByName(String roleName) {
        return sysRoleMapper.findRoleByName(roleName);
    }

 //  @Cacheable(value = "role")
    @Override
    public List<SysMenu> findRoleMenus(Long roleId) {
     //   SysRole sysRole = sysRoleMapper.findRoleById(roleId);
      //  if (SysConstant.ADMIN.equalsIgnoreCase(sysRole.getRoleName())) {
        if(roleId ==1){
            //如果是超级管理员，返回全部
            return sysMenuMapper.findAll();
        }
        List<SysMenu> list=sysMenuMapper.findRoleMenus(roleId);
        List<SysMenu> list1= sysMenuMapper.findRoleMenus(roleId);
        List<SysMenu> list2 = new ArrayList<>();
        for(SysMenu sysMenu:list){
            for(SysMenu sysMenu1:list1){
                if(sysMenu.getId().equals( sysMenu1.getParentId())){
                    list2.add(sysMenu);
                }
            }
        }
        list.removeAll(list2);
        return list;
    }

  //  @CachePut(value = "role")
    @Override
    public int saveRoleMenu(List<SysRoleMenu> records) {
        if(records == null || records.isEmpty()){
            return 1;
        }
        Long roleId = records.get(0).getRoleId();
        sysRoleMenuMapper.deleteByRoleId(roleId);
        for(SysRoleMenu record:records){

            sysRoleMenuMapper.insertSelective(record);
        }
        return 1;
    }

   // @Cacheable(value = "role")
    @Override
    public List<SysRole> findAll() {
        return sysRoleMapper.findAll();
    }

 //   @Cacheable(value = "role")
    @Override
    public PageResult findPageBy(PageRequest pageRequest, Long id) {
        PageResult pageResult=null;
        Object roleName =  pageRequest.getParam("roleName");
        Map<Object, Object> params = new HashMap<>();
        params.put("uid",id);
        pageRequest.setParams(params);
        Object uid = pageRequest.getParam("uid");

        if(id == 1){
            if(!"".equals(roleName)){
                pageResult = MybatisPageHelper.findPage(pageRequest, sysRoleMapper, "findRoleByName", roleName);
            }else {
                return MybatisPageHelper.findPage(pageRequest, sysRoleMapper,"findAllPage");
            }
        }else {
            if (!"".equals(roleName)) {
                pageResult = MybatisPageHelper.findPage(pageRequest, sysRoleMapper, "findRolePageByList", roleName, uid);
            } else {
                // return MybatisPageHelper.findPage(pageRequest, sysUserMapper);
                return MybatisPageHelper.findPage(pageRequest, sysRoleMapper, "findRolePageList", uid);
            }
        }
        return pageResult;
    }

    @Override
    public List<SysRole> findSelfAll(Long id) {
        return sysRoleMapper.findRolePageList(id);
    }

    @Override
    public SysRole findRoleNameById(String roleName, Long id) {
        SysRole sysRole = sysRoleMapper.findRoleNameById(roleName,id);
        return sysRole;
    }
}
